import pymysql
import asyncio
# 打开数据库连接
from aiohttp import ClientSession

db_config = {
    'host': 'rm-bp1f9kd27f51o2tm5ao.mysql.rds.aliyuncs.com', # 主机
    'user': 'tanfeng',       #用户名
    'password': 'xvjiYQEpt37mThx',  #密码
    'port': 3306,         #端口 3306
    'database':'wucang'   #数据库名
}

async def main():
        db = pymysql.connect(**db_config)
        # 使用cursor()方法获取操作游标
        cursor = db.cursor()
        # SQL 查询语句
        sql = "select orderId,`o_u_id`,`c_u_id`,`p_u_id`,`m_u_id` from temp_mh"
        try:
            # 执行SQL语句
            tf = 0
            cursor.execute(sql)
            # 获取所有记录列表
            results = cursor.fetchall()
            for row in results:
                orderid = row[0]
                o_u_id = row[1]
                c_u_id = row[2]
                cc_u_id = row[3]
                m_u_id = row[4]
                a = [o_u_id, c_u_id, cc_u_id,m_u_id]
                count = 0  # 计数置为1
                for i in range(0, len(a)):
                    one = a[i]
                    # 内层循环从第i个下标开始检测后三位是否连续
                    countd = 0
                    for j in range(len(a)):
                        if one == a[j]:
                            countd+=1
                        if countd == 4:
                            count+=1

                if count == 4 :
                    tf += 1
                    print(orderid)
                    sql = "INSERT INTO temp_order(orderId) VALUES ('%s');"
                    cursor.execute(sql % orderid)
                    db.commit()



        except:
            print("Error: unable to fetch data")

        # 关闭数据库连接
        print(tf)
        db.close()

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())
